module utils {
	/**
	 * @Date 2016/6/6
	 * @author speed
	 * 声音utils
	 *
	 */
	export class SoundUtil {
        public static soundOff:string = "off";
        public static soundOn:string = "on";

        private static maxNum: number = 100;
        private static effSoundDic: GameDict = new GameDict();
        private static bgSoundDic: GameDict = new GameDict();
        private static currBgSoundChannel: data.SoundInfo;

        private static _enableSound:boolean=true;

        private static _scList:Array<data.SoundInfo>=[];

        public static addSoundInfo(sc:data.SoundInfo)
        {
            SoundUtil._scList.push(sc);
        }
        public static removeSoundInfo(sc:data.SoundInfo)
        {
            var index=SoundUtil._scList.indexOf(sc);
            if(index!=-1)
                SoundUtil._scList.splice(index,1);
        }

        public static init()
        {
            egret.localStorage.setItem("bgSound",null);
            SoundUtil._enableSound=App.getItem(App.soundStorageKey)!=SoundUtil.soundOff;
        }

        public static getSoundEnable()
        {
            return SoundUtil._enableSound;
        }

        public static enableSound(value:boolean):void
        {
            if(SoundUtil._enableSound!=value)
            {
                SoundUtil._enableSound=value;

                for(var i=0;i<SoundUtil._scList.length;i++)
                {
                    var si:data.SoundInfo=SoundUtil._scList[i];
                    if(si&&si.soundChannel)
                    {
                        try{
                            si.soundChannel.volume=SoundUtil._enableSound?si.volume:0;
                        }catch(e){
                        }
                    }
                    else
                    {
                        SoundUtil._scList.splice(i,1);
                        i--;
                    }
                }
            }
        }
        
        public static getEffSound(mp3: string,cb: Function,needLoad: boolean = true): void {
            var self = SoundUtil;
            var onGetRes: Function = function(soundData: any) {
                if(soundData) {
                    var info: data.SoundInfo = new data.SoundInfo();
                    info.sound = soundData;
                    self.effSoundDic.put(mp3,info);
                    if(self.effSoundDic.length > self.maxNum) {
                        self.effSoundDic.shift();
                    }
                }
                cb(info);
            }
            if(needLoad) {
                if(RES.hasRes(mp3)) {
                    RES.getResAsync(mp3,onGetRes,self);
                }
                else {
                    // var mp3Name = mp3.split("_")[0];
                    // var mp3Res = "resource/assets/sound/effext/" + mp3Name + ".mp3";
                    RES.getResByUrl(mp3,onGetRes,self,RES.ResourceItem.TYPE_SOUND);
                }
            }
        }

        public static getBgSound(mp3: string,cb: Function): void {
            var self = SoundUtil;
            var onGetRes: Function = function(soundData: any) {
                if(soundData) {
                    var info: data.SoundInfo = new data.SoundInfo();
                    info.sound = soundData;
                    self.bgSoundDic.put(mp3,info);
                    if(self.bgSoundDic.length > self.maxNum) {
                        self.bgSoundDic.shift();
                    }
                }
                cb(info);
            }
            if(self.bgSoundDic.get(mp3)) {
                cb(self.bgSoundDic.get(mp3));
            }
            else {
                if(RES.hasRes(mp3)) {
                    RES.getResAsync(mp3,onGetRes,self);
                }
                else {
                    RES.getResByUrl(mp3,onGetRes,self,RES.ResourceItem.TYPE_SOUND);
                }
            }
        }
        
        /*
         * 播放音效
         */
        public static playEffSound(mp3: string,isRepeat: boolean = false,volume?: number): void {
            var self = SoundUtil;
            var soundState = App.getItem(App.soundStorageKey);
            if(mp3){
                self.getEffSound(mp3,function(data) {
                    if(data) {
                        var info: data.SoundInfo = data;
                        var sound: egret.Sound = info.sound;
                        if(sound) {
                            sound.type = egret.Sound.EFFECT;
                            info.volume=volume||1;
                            info.play(isRepeat);
                        }
                    }
                });
            }                           
        }
        
        /*
         * 播放背景音乐
         */
        public static playBgSound(mp3?:string,volume?: number): void {
            var self = SoundUtil;
            var soundState = App.getItem(App.soundStorageKey);
            if(mp3){
                var sound: egret.Sound = RES.getRes(mp3);
                if(self.currBgSoundChannel&&self.currBgSoundChannel.soundChannel) {
                    self.currBgSoundChannel.soundChannel.stop();
                }
                self.getBgSound(mp3,function(data,url) {
                    if(self.currBgSoundChannel&&self.currBgSoundChannel.soundChannel) {
                        self.currBgSoundChannel.soundChannel.stop();
                    }
                    if(data) {
                        var info: data.SoundInfo = data;
                        var sound: egret.Sound = info.sound;
                        if (sound) {
                            sound.type = egret.Sound.MUSIC;
                            info.volume=volume||0.5;
                            info.play(true);
                            self.currBgSoundChannel = info;
                        }
                    }
                });
                egret.localStorage.setItem("bgSound",mp3);
            }
        }
        
        /*
         * 停止背景音乐
         */
        public static stopBgSound(removeFlag:boolean=true): void {
            var self = SoundUtil;
            if(self.currBgSoundChannel) {
                self.currBgSoundChannel.stop();
            }
            if(removeFlag)
                egret.localStorage.setItem("bgSound",null);
        }
    }
}

module data {
    export class SoundInfo {
        public sound: egret.Sound;
        public soundChannel: egret.SoundChannel;
        public volume:number=1;
        protected _repeat:boolean=false;

        constructor()
        {
            utils.SoundUtil.addSoundInfo(this);
        }

        public play(repeat:boolean):egret.SoundChannel
        {
            if(!this.sound)
                return;
            this._repeat=repeat;
            this.soundChannel=this.sound.play(0,1);
            this.soundChannel.addEventListener(egret.Event.SOUND_COMPLETE,this.onSoundOver,this);
            this.soundChannel.volume=utils.SoundUtil.getSoundEnable()?this.volume:0;
            return this.soundChannel;
        }

        public stop()
        {
            if(!this.sound)
                return;
            this.soundChannel.stop();
        }

        protected onSoundOver(e:egret.Event)
        {
            if(this._repeat)
                this.play(true);
        }

        public despose(): void {
            var self = this;
            if(self.sound) {
                self.sound.close();
            }
            if(self.soundChannel) {
                self.soundChannel.stop();
            }
            utils.SoundUtil.removeSoundInfo(this);
        }
    }
}